package com.oocl.euc.core.requestform.service;

import com.oocl.euc.core.requestform.model.RequestSeqConfig;
import com.oocl.euc.core.requestform.repository.RequestSeqConfigRepository;
import com.oocl.euc.framework.base.persistence.IBaseRepository;
import com.oocl.euc.framework.base.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RequestSeqConfigService extends BaseService<RequestSeqConfig, Long> {

    @Autowired
    RequestSeqConfigRepository requestSeqConfigRepository;

    @Override
    protected IBaseRepository<RequestSeqConfig, Long> getRepository() {
        return requestSeqConfigRepository;
    }

    @Transactional(rollbackFor = Exception.class)
    public RequestSeqConfig generateRequestSeq(String prefix) {

        RequestSeqConfig requestSeqConfig = requestSeqConfigRepository.findByPrefix(prefix);
        long currentNo = requestSeqConfig.getCurrentNo();
        requestSeqConfig.setCurrentNo(currentNo + 1);
        return requestSeqConfigRepository.save(requestSeqConfig);
    }

}
